package com.zhuguowei.mytoast;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 项目名称:MyToast
 * 创建人:zhuguowei
 * 创建时间:2018/4/18 8:47
 */
public class ToastUtils {
    private Toast mToast;
    private LinearLayout mToastView;

    public ToastUtils() {
    }

    /**
     * 完全自定义布局Toast
     * @param context
     * @param view
     * @param duration
     */
    public ToastUtils(Context context, View view, int duration) {
        mToast = new Toast(context);
        mToast.setView(view);
        mToast.setDuration(duration);
    }

    /**
     * 向Toast中添加自定义View
     * @param view
     * @param position
     * @return
     */
    public ToastUtils addView(View view, int position) {
        mToastView = (LinearLayout) mToast.getView();
        mToastView.addView(view, position);
        return this;
    }

    /**
     * 设置字体颜色和背景颜色
     * @param messageColor
     * @param backgroundColor
     * @return
     */
    public ToastUtils setToastColor(int messageColor, int backgroundColor) {
        View view = mToast.getView();
        if (view != null) {
            TextView message = view.findViewById(android.R.id.message);
            message.setTextColor(messageColor);
            view.setBackgroundColor(backgroundColor);
        }
        return this;
    }

    /**
     * 设置字体及背景
     * @param messageColor
     * @param background
     * @return
     */
    public ToastUtils setToastBackground(int messageColor, int background) {
        View view = mToast.getView();
        if (view != null) {
            TextView message = view.findViewById(android.R.id.message);
            message.setTextColor(messageColor);
            message.setPadding(50,0,50,0);
            view.setBackgroundResource(background);
        }
        return this;
    }

    /**
     * 短时间显示Toast
     * @param context
     * @param message
     * @return
     */
    public ToastUtils Short(Context context, CharSequence message) {
        if (mToast == null || (mToastView != null && mToastView.getChildCount() > 1)) {
            mToast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
            mToastView = null;
        } else {
            mToast.setDuration(Toast.LENGTH_SHORT);
            mToast.setText(message);
        }
        return this;
    }

    /**
     * 短时间显示Toast
     * @param context
     * @param message
     * @return
     */
    public ToastUtils Short(Context context, int message) {
        if (mToast == null || (mToastView != null && mToastView.getChildCount() > 1)) {
            mToast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
            mToastView = null;
        } else {
            mToast.setDuration(Toast.LENGTH_SHORT);
            mToast.setText(message);
        }
        return this;
    }

    /**
     * 长时间显示Toast
     * @param context
     * @param message
     * @return
     */
    public ToastUtils Long(Context context, CharSequence message) {
        if (mToast == null || (mToastView != null && mToastView.getChildCount() > 1)) {
            mToast = Toast.makeText(context, message, Toast.LENGTH_LONG);
            mToastView = null;
        } else {
            mToast.setDuration(Toast.LENGTH_LONG);
            mToast.setText(message);
        }
        return this;
    }

    /**
     * 长时间显示Toast
     * @param context
     * @param message
     * @return
     */
    public ToastUtils Long(Context context, int message) {
        if (mToast == null || (mToastView != null && mToastView.getChildCount() > 1)) {
            mToast = Toast.makeText(context, message, Toast.LENGTH_LONG);
            mToastView = null;
        } else {
            mToast.setDuration(Toast.LENGTH_LONG);
            mToast.setText(message);
        }
        return this;
    }

    /**
     * 自定义显示时间
     * @param context
     * @param message
     * @return
     */
    public ToastUtils setDuration(Context context, CharSequence message, int duration) {
        if (mToast == null || (mToastView != null && mToastView.getChildCount() > 1)) {
            mToast = Toast.makeText(context, message, duration);
            mToastView = null;
        } else {
            mToast.setDuration(duration);
            mToast.setText(message);
        }
        return this;
    }

    /**
     * 自定义显示时间
     * @param context
     * @param message
     * @return
     */
    public ToastUtils setDuration(Context context, int message, int duration) {
        if (mToast == null || (mToastView != null && mToastView.getChildCount() > 1)) {
            mToast = Toast.makeText(context, message, duration);
            mToastView = null;
        } else {
            mToast.setDuration(duration);
            mToast.setText(message);
        }
        return this;
    }

    /**
     * 显示Toast
     * @return
     */
    public ToastUtils show() {
        mToast.show();
        return this;
    }

    /**
     * 获取Toast
     * @return
     */
    public Toast getToast() {
        return mToast;
    }
}
